package com.atguigu.gmall.cart.rest;

import com.atguigu.gmall.cart.entity.CartInfo;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:lyd
 * @Date:2023/9/2 0:20
 * @Version:1.0
 * @Description
 **/
@RestController
@RequestMapping("/api/cart")
public class CartRestController {

    @Autowired
    CartService cartService;

    /**
     * 添加到购物车的商品展示列表
     *
     * @return
     */
    @GetMapping("/cartList")
    public Result cartList() {
        String cartKey = cartService.determineKey();
        List<CartInfo> list = cartService.list(cartKey);
        return Result.ok(list);
    }


    /**
     * 改变选中状态
     *
     * @param skuId
     * @param checked
     * @return
     */
    //http://api.gmall.com/api/cart/checkCart/50/1
    @GetMapping("/checkCart/{skuId}/{checked}")
    public Result checkCart(@PathVariable Long skuId,
                            @PathVariable Integer checked) {
        String cartKey = cartService.determineKey();
        cartService.checkCart(skuId, checked, cartKey);
        return Result.ok();
    }


    /**
     * 从购物车删除单个商品
     *
     * @param skuId
     * @return
     */
    //http://api.gmall.com/api/cart/deleteCart/49
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(@PathVariable Long skuId) {
        String cartKey = cartService.determineKey();
        cartService.deleteCart(skuId, cartKey);
        return Result.ok();
    }


    /**
     * 操作+或-
     * @param skuId
     * @param num
     * @return
     */
    //http://api.gmall.com/api/cart/addToCart/50/1
    @PostMapping("/addToCart/{skuId}/{num}")
    public Result addToCart(@PathVariable Long skuId,
                            @PathVariable Integer num) {
        String cartKey = cartService.determineKey();
        cartService.addToCart(skuId, num, cartKey);
        return Result.ok();
    }
}
